package cart;

import java.io.Serializable;
import java.security.*;

public class Authentication implements Serializable {
    String id;
    String pwd;
    String dbpwd;
    
    public Authentication(String userID, String password) {
        id = userID.toLowerCase();
        pwd = password;
    }
    public void setdbpwd(String databasePassword){
        dbpwd = databasePassword;
    }
    public void setpwd(String password){
        pwd = password;
    }
    
    private String hash(String str) {
        try {
           MessageDigest mdAlgorithm = MessageDigest.getInstance("MD5");
           mdAlgorithm.update(str.getBytes());
           byte[] digest = mdAlgorithm.digest();
           StringBuilder hexString = new StringBuilder();
           for (int i = 0; i < digest.length; i++) {
                str = Integer.toHexString(0xFF & digest[i]);
                if (str.length() < 2) {
                    str = "0" + str;
                }
                hexString.append(str);
           }         
           return hexString.toString();
        }
        catch (NoSuchAlgorithmException e)
        {
            return null;
        }
    }
    public String hashPwd() {
          pwd = "AST20201" + pwd + id;
          pwd = hash(pwd);
          return pwd;
    }
    
     public boolean chekLogin(){
        if (pwd.equals(dbpwd))
            return true;
        return false;
    }
    
}
